<?php
/**
 * Created by PhpStorm.
 * User: MI
 * Date: 2018/5/25
 * Time: 14:12
 */

namespace App\Http\Controllers\API;


use App\Http\Controllers\Controller;
use App\Models\Student;
use App\Models\StudentInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudentsInfoController extends Controller
{
    public function index(Request $request)
    {
        $queryString = $request->get('query', '');
        $selectString = $request->get('select', '');
        $models = StudentInfo::query()->when($queryString, function ($q) use ($queryString) {
            $q->where('xm', 'like', '%' . $queryString . '%');
        })->when($selectString, function ($q) use ($selectString) {
            $q->where('status', $selectString);
        })
            ->paginate();
        return $models;
    }


    /**
     * 添加科室
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = new StudentInfo();
        $model->fill($request->all());
        if ($model->save()) {
            return $model;
        } else {
            return response($model->getErrors()->toArray(), 406);
        }
    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update(StudentInfo $StudentLog, Request $request)
    {
        $StudentLog->fill($request->all());
        if ($StudentLog->save()) {
            return $StudentLog;
        } else {
            return response($StudentLog->getErrors()->toArray(), 406);
        }
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            $model->delete();
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        return $this->findModel($id);
    }


    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = StudentInfo::with(['xshschool', 'nation', 'political'])->findOrFail($id);
        return $model;
    }

    /**
     * 更具openid 查看用户详细信息
     * @param Request $request
     * @return StudentInfo|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Http\JsonResponse|null
     */
    public function getStudentInfoByOpenid(Request $request)
    {
        \DB::beginTransaction();
        try {
            $wx_key = $request->get('wx_key');
            $student = Student::query()->where('wx_key', $wx_key)->first();
            if (!$student) {
                throw new \Exception('未找到该学生');
            }
            $info = StudentInfo::with(['xshschool', 'nation', 'political'])->where('student_id', $student->id)->first();
            if (!$info) {
                throw new \Exception('不符合参与条件');
            }
            return $info;
        } catch (\Exception $e) {
            return response()->json($e->getMessage(), 454);
        }
    }

    /**
     * 确认信息
     * @param $id
     * @return mixed
     */
    public function confirmUserInfo($id)
    {
        $model = $this->findModel($id);
        $model->status = 2;
        $model->save();
        return ['state' => 'success'];
    }

    /**
     * 修正信息
     * @param Request $request
     * @return mixed
     */

    public function correctUserInfo(Request $request)
    {
        $data = $request->all();
        $model = $this->findModel($data['id']);
        $model->status = 3;
        $model->update_desc = $data['desc'];
        $model->save();
        return ['state' => 'success'];
    }


    public function exportAll()
    {

        $cellData = [
            ['姓名', '学号', '性别', '出生日期', '身份证号', '民族', '政治面貌', '班级', '联系方式', '专业', '学籍校', '认证状态', '提交修正信息']
        ];
        $statusArr = [
            1 => '尚未认证',
            2 => '确认吻合',
            3 => '提交修正',
        ];
        $query = StudentInfo::with(['xshschool', 'nation', 'political'])->get();
        foreach ($query as $item) {
            $res[] = $item->xm;
            $res[] = $item->xh;
            $res[] = $item->xbm;
            $res[] = $item->csrq;
            $res[] = " ".$item->sfzjh;
            $res[] = $item->nation ? $item->nation->value : '无';
            $res[] = $item->political ? $item->political->value : '无';
            $res[] = $item->ywb;
            $res[] = $item->lxdh;
            $res[] = $item->zyfx;
            $res[] = $item->school ? $item->xshschool->value : '无';
            $res[] = $statusArr[$item->status];
            $res[] = $item->update_desc;
            $cellData[] = $res;
            unset($res);
        }
        \Excel::create('全部人员信息', function ($excel) use ($cellData) {
            $excel->sheet('sheet', function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }


    public function exportCorrect()
    {

        $cellData = [
            ['姓名', '学号', '性别', '出生日期', '身份证号', '民族', '政治面貌', '班级', '联系方式', '专业', '学籍校', '认证状态', '提交修正信息']
        ];
        $statusArr = [
            1 => '尚未认证',
            2 => '确认吻合',
            3 => '提交修正',
        ];
        $query = StudentInfo::with(['xshschool', 'nation', 'political'])->where('status',3)->get();
        foreach ($query as $item) {
            $res[] = $item->xm;
            $res[] = $item->xh;
            $res[] = $item->xbm;
            $res[] = $item->csrq;
            $res[] = " ".$item->sfzjh;
            $res[] = $item->nation ? $item->nation->value : '无';
            $res[] = $item->political ? $item->political->value : '无';
            $res[] = $item->ywb;
            $res[] = $item->lxdh;
            $res[] = $item->zyfx;
            $res[] = $item->school ? $item->xshschool->value : '无';
            $res[] = $statusArr[$item->status];
            $res[] = $item->update_desc;
            $cellData[] = $res;
            unset($res);
        }
        \Excel::create('修正人员信息', function ($excel) use ($cellData) {
            $excel->sheet('sheet', function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}